<template>
  <div class="padding_10">
    <div class="grid_4_column">
      <!-- 客户姓名 -->
      <div class="flex_y_center">
        <div class="title_txt">客户姓名：</div>
        <el-input
          v-model="filterFrom.customer_name"
          placeholder="请输入客户姓名"
          style="width: 70%"
        />
      </div>
      <!-- 客户电话 -->
      <div class="flex_y_center">
        <div class="title_txt">客户电话：</div>
        <el-input
          v-model="filterFrom.customer_tel"
          placeholder="请输入客户电话"
          style="width: 70%"
        />
      </div>
      <!-- 单号 -->
      <div class="flex_y_center">
        <div class="title_txt">单号：</div>
        <el-input v-model="filterFrom.order_sn" placeholder="请输入单号" />
      </div>
      <!-- 类型 -->
      <div class="flex_y_center">
        <div class="title_txt">类型：</div>
        <el-select v-model="filterFrom.bill_type" placeholder="请选择类型">
          <el-option
            v-for="(item, key) of bill_type"
            :key="key"
            :label="item"
            :value="key"
          />
        </el-select>
      </div>
      <!-- 支付方式 -->
      <div class="flex_y_center">
        <div class="title_txt">支付方式：</div>
        <el-select v-model="filterFrom.payment_id" placeholder="请选择支付方式">
          <el-option
            v-for="(item, key) of payment"
            :key="key"
            :label="item"
            :value="key"
          />
        </el-select>
      </div>
      <!-- 支付状态 -->
      <div class="flex_y_center">
        <div class="title_txt">流水状态：</div>
        <el-select v-model="filterFrom.status" placeholder="请选择支付状态">
          <el-option
            v-for="(item, key) of status"
            :key="key"
            :label="item"
            :value="key"
          />
        </el-select>
      </div>
    </div>
    <!-- 筛选，重置按钮 -->
    <div style="margin: 10px 0">
      <el-button
        icon="el-icon-search"
        :loading="fiterLoad"
        type="primary"
        @click="getTransaction('fiterLoad')"
      >
        筛选
      </el-button>
      <el-button icon="el-icon-refresh" :loading="resetLoad" @click="reset">
        重置
      </el-button>
    </div>

    <el-table :data="tableData" :empty-text="emptyText">
      <el-table-column fixed label="类型" prop="bill_type_name" />
      <el-table-column fixed label="客户名称" prop="customer_name" />
      <el-table-column fixed label="客户电话" prop="customer_tel" width="120" />
      <el-table-column label="交易时间" prop="created_at" width="200" />
      <el-table-column label="业务单号" prop="order_info" width="200" />
      <el-table-column label="支付流水号" prop="transaction_sn" width="200" />
      <el-table-column label="流水状态">
        <template slot-scope="scope">
          {{ scope.row.status == 'success' ? '成功' : '失败' }}
        </template>
      </el-table-column>
      <el-table-column label="交易金额">
        <template slot-scope="scope">
          {{ scope.row.init_amt + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="实际金额">
        <template slot-scope="scope">
          {{ scope.row.trx_amt + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="支付方式" prop="payment_name" width="100" />
      <el-table-column align="center" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            v-permissions="{ permission: ['view:Transaction'] }"
            type="primary"
            @click="details(scope.row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="flex_between">
      <div></div>
      <el-pagination
        :current-page.sync="page"
        layout="prev, pager, next, jumper"
        :total="total"
        @current-change="getTransaction"
      />
    </div>

    <el-dialog title="流水详情" :visible.sync="dialog">
      <Details :o-flow-details="flowDetails" />
      <div slot="footer">
        <el-button type="danger" @click="dialog = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { transaction, transactionSearchItem } from '@/api/order'
  import Details from './components/details.vue'

  export default {
    components: {
      Details,
    },
    data() {
      return {
        fiterLoad: false,
        resetLoad: false,
        bill_type: [], // 类型
        payment: [], // 付款方式
        status: [], // 流水状态
        filterFrom: {
          bill_type: '', // 账单类型
          transaction_sn: '', // 流水号
          order_sn: '', // 订单号
          payment_id: '', // 支付方式ID
          customer_tel: '', // 客户电话
          customer_name: '', // 客户姓名{模糊匹配}
          status: '', // 流水状态
        },
        emptyText: ' ',
        tableData: [],
        page: 1,
        total: 10,
        dialog: false,
        flowDetails: {},
      }
    },
    mounted() {
      this.getTransaction()
      this.getTransactionSearchItem()
    },
    methods: {
      // 流水日志
      getTransaction(e) {
        this[e] = true
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })

        let data = {
          page: this.page,
        }
        data = { ...data, ...this.filterFrom }

        transaction(data).then((res) => {
          this.tableData = res.data.data
          this[e] = false
          loading.close()
          this.emptyText = '暂无数据'
          this.total = res.data.last_page * 10
        })
      },
      // 获取流水日志搜索参数
      getTransactionSearchItem() {
        transactionSearchItem().then((res) => {
          this.bill_type = res.data.bill_type
          this.payment = res.data.payment
          this.status = res.data.status
        })
      },
      // 重置筛选条件，重新获取列表
      reset() {
        for (let item in this.filterFrom) {
          this.filterFrom[item] = ''
        }

        this.getTransaction('resetLoad')
      },
      details(row) {
        this.flowDetails = row
        this.dialog = true
      },
    },
  }
</script>

<style scoped>
  .grid_4_column {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-gap: 10px;
  }
  .flex_between {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .title_txt {
    width: 80px;
    text-align: right;
  }
</style>
